$(function () {
    var member_datagrid = $("#member_datagrid");
    var member_dialog = $("#member_dialog");
    var editForm = $("#editForm");
    $.get("/member/getTotalNum.do", function (data) {
        $("#totalMem").html(data);
    })
    $.get("/member/getBrithMenBer.do", function (data) {
        $("#brithday").html(data[0]);
        $("#brithmonth").html(data[1]);
    })
    $.get("/member/getTotalSpend.do", function (data) {
        $("#totalA").html(data);
    })
    $.get("/member/getMax.do", function (data) {
        console.log(data);
        $("#num1").html(data[0].member.name);
        $("#num2").html(data[1].member.name);
        $("#num3").html(data[2].member.name);
        $("#money1").html(data[0].totalbalance);
        $("#money2").html(data[1].totalbalance);
        $("#money3").html(data[2].totalbalance);
    })
    $('#mem_brithday').panel({
        width: 500,
        height: 150,
    });
    //初始化 卡号 姓名 电话号必填-----------|
    $("#moveMemBer").combobox({
        url: '/member/getAllGroup.do',
        valueField: 'id',
        textField: 'groupname',
        panelHeight: 'auto',
        onChange: function (newValue, oldValue) {
            //判断是否选中数据
            var row = member_datagrid.datagrid("getSelected");
            if (!row) {
                //提示选择数据
                $.messager.alert('温馨提示', '至少选择一条编辑数据', "info");
                return;
            }
            $.get("/member/moveMember.do?memberId=" + row.id + "&groupId=" + newValue, function (data) {
                if (data.success) {
                    //提示成功
                    $.messager.confirm('温馨提示', "操作成功", function (r) {
                        //刷新数据
                        member_datagrid.datagrid("reload");
                    });
                } else {
                    $.messager.alert('温馨提示', data.msg, "error");
                }
            })
        }

    })
    $('#cardNum').validatebox({
        required: true
    });
    $('#name').validatebox({
        required: true
    });
    $('#tel').validatebox({
        required: true,
        length: [11, 11]
    });
    //----------------------------------|
    //初始化性别--------------------------|
    $('#sex').combobox({
        valueField: 'key',
        textField: 'value',
        panelHeight: 'auto',
        data: [{
            key: 'false',
            value: '先生'
        }, {
            key: 'true',
            value: '女士'
        }]
    });
    //----------------------------------|
    //初始化等级--------------------------|
    $('#memberGrade').combobox({
        url: '/member/getAllGrade.do',
        valueField: 'id',
        textField: 'gradename',
        panelHeight: 'auto'
    });
    //----------------------------------|
    //初始化分组--------------------------|
    $('#memberGroup').combobox({
        url: '/member/getAllGroup.do',
        valueField: 'id',
        textField: 'groupname',
        panelHeight: 'auto'
    });
    //----------------------------------|
    //会员列表初始化----------------------|
    member_datagrid.datagrid({
        fit: true,
        fitColumns: false,
        striped: true,
        url: "/member/list.do",
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        toolbar: "#tb",
        columns: [[
            {field: "cb", checkbox: true},
            {field: "id", hidden: true},
            {field: "cardNum", title: "卡号", width: 184},
            {field: "name", title: "会员名称", width: 184, formatter: nameFormatter},
            {field: "tel", title: "电话号", width: 184},
            {field: "group", title: "分组", width: 184, formatter: groupFormatter},
            {field: "memberGrade", title: "会员等级", width: 184, formatter: gradeFormatter},
            {field: "integral", title: "可用积分", width: 184},
            {field: "balance", title: "储值余额", width: 184},
            {field: "address", title: "客户地址", width: 184}
        ]]
    })
    //----------------------------------|
    //生日框-----------------------------|
    $('#birthday').datebox({
        required: true
    });
    //----------------------------------|
    //对话框初始化------------------------|
    member_dialog.dialog({
        width: 300,
        height: 600,
        buttons: "#bb",
        closable: true,
        closed: true
    })
    //----------------------------------|


    var cmdObj = {
        //会员添加
        add: function () {
            //打开弹出框
            member_dialog.dialog("open");
            //设置标题投
            member_dialog.dialog("setTitle", "会员添加");
            //清除缓存数据
            editForm.form("clear");
        },
        //会员编辑
        edit: function () {
            //判断是否选中数据
            var row = member_datagrid.datagrid("getSelected");
            if (!row) {
                //提示选择数据
                $.messager.alert('温馨提示', '至少选择一条编辑数据', "info");
                return;
            }
            //打开弹出框
            member_dialog.dialog("open");
            //设置标题投
            member_dialog.dialog("setTitle", "会员编辑");
            //清除缓存数据
            editForm.form("clear");
            //构建会员回显数据
            if (row.group) {
                row["group.id"] = row.group.id;
            }
            if (row.memberGrade) {
                row["memberGrade.id"] = row.memberGrade.id;
            }
            if (row.sex) {
                row["sex"] = "女士";
            } else {
                row["sex"] = "先生";
            }
            //数据回显
            editForm.form("load", row);
        },
        //----------------------------------|
        //会员保存/更新
        save: function () {
            //表单提交url
            var url;
            //使用id区分保存于更新操作
            var id = $("#empId").val();
            url = id ? "/member/edit.do" : "/member/save.do";
            //提交表单
            editForm.form("submit", {
                url: url,
                success: function (data) {
                    //jquery将字符串转换json对象
                    //data必须是标准的json字符串
                    data = $.parseJSON(data);
                    if (data.success) {
                        //提示成功
                        $.messager.confirm('温馨提示', "操作成功", function (r) {
                            //关闭窗口
                            member_dialog.dialog("close");
                            //刷新数据
                            member_datagrid.datagrid("reload");

                        });
                    } else {
                        $.messager.alert('温馨提示', data.msg, "error");
                    }
                }

            })
        },
        searchT: function () {
            var kw = $("#keyword").val();
            //加载列表数据时,携带额外参数
            member_datagrid.datagrid("load", {
                keyword: kw
            })
        },
        //----------------------------------|
        //会员删除
        remove: function () {
            //判断是否选中数据
            var row = member_datagrid.datagrid("getSelected");
            if (!row) {
                //提示选择数据
                $.messager.alert('温馨提示', '至少选择一条要删除数据', "info");
                return;
            }
            $.messager.confirm('温馨提示', "您确定要删除么?", function (ret) {
                if (ret) {
                    //确定删除
                    $.get("/member/delete.do?id=" + row.id, function (data) {

                        if (data.success) {
                            //删除成功
                            $.messager.confirm('温馨提示', "删除成功", function (r) {
                                //刷新数据
                                member_datagrid.datagrid("reload");
                            });
                        } else {
                            $.messager.alert('温馨提示', data.msg, "error");
                        }

                    })
                }

            });
        },
        //----------------------------------|
        //刷新
        reload: function () {
            member_datagrid.datagrid("reload");
        },
        //----------------------------------|
        //对话框取消
        cancel: function () {
            //关闭窗口
            member_dialog.dialog("close");
        }
        //----------------------------------|
    }
    //给按钮绑定事件
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    })
    //----------------------------------|
})

function groupFormatter(value, row, index) {
    return value ? value.groupname : "";

}

function gradeFormatter(value, row, index) {
    return value ? value.gradename : "";
}

function nameFormatter(value, row, index) {
    return value + "(" + (row.sex ? "女士" : "先生") + ")";
}
